<?php
namespace App\Models;
use Symfony\Component\HttpFoundation\Request;

/**
 * 系统内容分类对象
 */
class Frag extends BaseModel
{
    public $table = "frag";

    /**
     * 碎片信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function FragRulesWithMsg($flag)
    {
        $rules = array(
            "position"  => "required|max:100",
            'slug'  => 'required|max:100',

        );

        $msg = array(
            'position.required' => '碎片位置必须填写',
            'position.max'       => '碎片位置最大不能超过100个字符',
            'slug.required'  => '碎片标示必须填写',
            'slug.max'        => '碎片标示最大不能超过100个字符',
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 碎片内容保存
     */
    public static function saveFrag (Request $request,$frag)
    {
        $frag->position = $request->position;
        $frag->slug = $request->slug;
        if($request->hasFile("picture")){
            $suffix = $request->file('picture')->getClientOriginalExtension();

            //写入本地
            $request->file('picture')->move('system/images/',$request->slug.'.'.$suffix);
        }

        if(!empty($request->picture)){
            $frag->picture = 'system/images/'.$request->slug.'.'.$suffix;
        }

        $frag->title = strip_tags($request->title);
        $frag->content = strip_tags($request->content);
        $frag->create_time = time();
        $frag->create_addr =strip_tags($request->create_addr);

        return $frag->save();
    }

}